package nc.pub.hrpbm.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import nc.bs.fileupload.itf.IBipFileUploadService;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.vo.fourcgxm.fourcgxm.AggFourcgxmVO;
import nc.vo.fourcgxm.fourcgxm.FourcgxmVO;
import nc.vo.pub.BusinessException;

import java.util.ArrayList;
import java.util.List;

public class CGFileUtil {
    private IFileSystemService fsService;
    private IBipFileUploadService fileUploadService;

    /**
     * 附件上传采购项目
     * @throws BusinessException
     */
    public static void handCGAggVOs(AggFourcgxmVO[] aggvos)  throws BusinessException {
        CGFileUtil util = new CGFileUtil();
        for (AggFourcgxmVO aggvo : aggvos) {
            FourcgxmVO hvo = aggvo.getParentVO();
            String pk_bill = hvo.getPk_fourcgxm();
            String vbillcode = hvo.getHopbillcode();
            String pk_group  = hvo.getPk_group();
            String pk_org = hvo.getPk_org();
            String pk_billtypeid = hvo.getBilltype();
            String userid = hvo.getCreator();
            String fileName = "采购系统";
            Object Attach = hvo.getAttributeValue("attach");
            if(null != Attach) {
                List<String> attchmentsurls = new ArrayList<>();
                JSONArray attachs = JSON.parseArray(Attach.toString());
                if(attachs != null && attachs.size() > 0) {
                    for (Object attach : attachs) {
                        attchmentsurls.add(attach.toString());
                    }
                }
                if(attchmentsurls.size() > 0) {
                    util.getFsService().createNewFileNode(pk_bill, fileName, userid);
                    String fullPath = pk_bill+"/"+fileName;
                    util.getFileUploadService().fileUploadNC(fullPath, pk_bill, vbillcode, attchmentsurls, pk_group, pk_org, pk_org, pk_billtypeid, userid);

                }
            }
        }
    }

    public IFileSystemService getFsService() {
        if (null == this.fsService) {
            this.fsService = NCLocator.getInstance().lookup(IFileSystemService.class);
        }
        return this.fsService;
    }

    public IBipFileUploadService getFileUploadService() {
        if (null == this.fileUploadService) {
            this.fileUploadService = NCLocator.getInstance().lookup(IBipFileUploadService.class);
        }
        return fileUploadService;
    }
}
